#include "snake.h"

//控制光标
void setCursorPosition(const int x, const int y) {
    COORD coord;
    coord.X = (x << 1);//抵消宽高差
    coord.Y = y;
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);//获取句柄
    SetConsoleCursorPosition(hConsole, coord);
}
// 隐藏控制台光标
void hideCursor() {
    CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

// 欢迎界面
void welcometogame() {
    system("cls");
    // 添加snake字符画
    // 更简单的snake字样
    cout << "\n\n";
    cout << "\t\t\t   ██████ ███    ██  █████  ██   ██ ███████\n";
    cout << "\t\t\t  ██      ████   ██ ██   ██ ██  ██  ██\n";
    cout << "\t\t\t  ███████ ██ ██  ██ ███████ █████   █████\n";
    cout << "\t\t\t       ██ ██  ██ ██ ██   ██ ██  ██  ██\n";
    cout << "\t\t\t  ██████  ██   ████ ██   ██ ██   ██ ███████\n";
    cout << "\n";


    // 显示当前最高分
    file_out(); // 确保读取最新最高分
    cout << "\t\t\t     当前最高分: " << game.MaxScore << "\n\n";
    cout << "\t\t\t  按Enter键开始游戏...\n";
    cout << "\t\t\t  按 'ESC' 退出游戏\n";
    cout << "\t\t\t  按 'W' 'S' 'A' 'D' 控制方向\n";
    cout << "\t\t\t  按 '+' 加速，按 '-' 减速\n";
    interfaceGetkeyborad();//主界面专用的键盘检测
}

// 游戏结束界面
void gameover() {
    system("cls");
    cout << "\n\n\n\n\n\n\n\n\n\n";
    cout << "\t\t\t 游戏结束！\n";
    cout << "\t\t\t 最终分数: " << game.score << endl;
    if (game.score > game.MaxScore) {
        cout << "\t\t\t 创纪录啦！最高分被你刷新啦，真棒！！！ " << endl;
    }
    else {
        cout << "\t\t\t 继续努力吧~你离最高分还差:" << game.MaxScore - game.score << endl;
    }
    cout << endl;
    cout << "\t\t\t 按Esc键返回主菜单" << endl;
    cout << "\t\t\t 按Enter键重新开始游戏";

    // 在游戏结束时检查并更新最高分
    if (game.score > game.MaxScore) {
        game.MaxScore = game.score;
        file_in(); // 立即保存新的最高分
    }

    gameoverGetkeyboard();
}

// 绘制地图
void createmap() {
    //清屏
    system("cls");
    for (int i = 0; i <= HEIGHT; i++) {
        setCursorPosition(i, 0);
        cout << "■";
        setCursorPosition(i, HEIGHT);
        cout << "■";
    }
    for (int i = 0; i <= HEIGHT; i++) {
        setCursorPosition(0, i);
        cout << "■";
        setCursorPosition(HEIGHT, i);
        cout << "■";
    }
    // 显示分数和提示

    scoreandtips();
}

// 显示分数和提示
void printScores() {
    setCursorPosition(0, HEIGHT + 1);
    cout << "当前分数:" << game.score << "  " << "下一次得分:" << game.ScorePerFood << "  " << "最高得分:" << game.MaxScore << "  " << endl;
    cout<< "当前速度:" << game.speed << "(数值越小速度越快)"<< "速度越快，单个食物奖励分数越多哦!" << endl;;
}
void scoreandtips() {
    setCursorPosition(0, HEIGHT + 3);
    cout << "控制: W(上) S(下) A(左) D(右)  +(加速) -(减速)  ESC(退出)" << endl;
}


